import { GameRes } from "../../GameRes";
import { DailyDetailsItemInfo } from "../Data/DailyDetailsItemInfo";
import { EventNames } from "../Data/EventNames";
import { DailyDetailsItem } from "./DailyDetailsItem";
import { ViewDailyGameRun } from "./ViewDailyGameRun";

const { regClass, property } = Laya;

@regClass()
export class DailyItem extends Laya.Script {
    /**日报内容 */
    private labelDailyTitle: Laya.Label = null;
    /**日报日期 */
    private labelDailyDate: Laya.Label = null;
    /**打开日报详情按钮 */
    private btnOpenDetails: Laya.Image = null;
    /**是否可以点击打开日报详情 */
    private isCanClickOpenDetails: boolean = false;
    /**日报详情信息 */
    private dailyDetailsItemInfo: DailyDetailsItemInfo = null;

    onAwake(): void {
        this.init();
        this.findComponent();
        this.addEventListener();
    }

    onDestroy(): void {
        this.removeEventListener();
    }

    public updateInfo(data: DailyDetailsItemInfo): void {
        this.dailyDetailsItemInfo = data;
        this.labelDailyTitle.text = data.dailyTitle;
        this.labelDailyDate.text = data.dailyDate;
    }

    private init(): void {
        this.isCanClickOpenDetails = true;
    }
    private findComponent(): void {
        this.labelDailyTitle = this.owner.getChildByName("labelDailyTitle") as Laya.Label;
        this.labelDailyDate = this.owner.getChildByName("labelDailyDate") as Laya.Label;
        this.btnOpenDetails = this.owner.getChildByName("btnOpenDetails") as Laya.Image;
    }

    private addEventListener(): void {
        this.btnOpenDetails.on(Laya.Event.CLICK, this, this.onClickOpenDetailsBtn);
        Laya.stage.on(EventNames.DAILY_DETAILS_HIDE, this, this.onHideDailyDetails);
    }

    private removeEventListener(): void {
        this.btnOpenDetails.off(Laya.Event.CLICK, this, this.onClickOpenDetailsBtn);
        Laya.stage.off(EventNames.DAILY_DETAILS_HIDE, this, this.onHideDailyDetails);
    }
    /**监听点击查看详情按钮 */
    private onClickOpenDetailsBtn(): void {
        if (!this.isCanClickOpenDetails) { return; }
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        // this.isCanClickOpenDetails = false;
        this.createDailyDetailsItem();
    }
    /**显示日报详情 */
    private createDailyDetailsItem(): void {
        Laya.stage.event(EventNames.DAILY_DETAILS_SHOW, this.dailyDetailsItemInfo);
    }
    /**隐藏日报详情 */
    private onHideDailyDetails(dailyId: number): void {
        // if (this.dailyDetailsItemInfo == null) { return; }
        // if (this.dailyDetailsItemInfo.dailyId != dailyId) { return; }
        // this.isCanClickOpenDetails = true;
    }
}